package dhm.com.hpx.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.MainArticleAdapter;
import dhm.com.hpx.adapter.MainPosterAdapter;
import dhm.com.hpx.adapter.MainVideoAdapter;
import dhm.com.hpx.adapter.UserListAdapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.entity.GetMyUserBean;
import dhm.com.hpx.entity.MainArticleBean;
import dhm.com.hpx.entity.MainPosterBean;
import dhm.com.hpx.entity.MainVideoBean;
import dhm.com.hpx.utils.SpUtils;

public class SearchActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.search)
    EditText search;
    @BindView(R.id.article_text)
    TextView articleText;
    @BindView(R.id.article_line)
    TextView articleLine;
    @BindView(R.id.video_text)
    TextView videoText;
    @BindView(R.id.video_line)
    TextView videoLine;
    @BindView(R.id.poster_text)
    TextView posterText;
    @BindView(R.id.poster_line)
    TextView posterLine;
    @BindView(R.id.user_text)
    TextView userText;
    @BindView(R.id.user_line)
    TextView userLine;
    @BindView(R.id.recy)
    XRecyclerView recy;
    private String uid;

    private PressenterImpl pressenter;

    private MainArticleAdapter mainArticleAdapter;
    private List<MainArticleBean.DataBean> mainArticleBeanData;
    private List<MainVideoBean.DataBean> mainVideoBeanData;
    private MainVideoAdapter mainVideoAdapter;
    private List<MainPosterBean.DataBean> mainPosterBeanData;
    private MainPosterAdapter mainPosterAdapter;
    private int page=1;
    private String keywords;
    private List<GetMyUserBean.DataBean> userListBeanData;
    private UserListAdapter userListAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

        uid = SpUtils.getString(this, "uid");
        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);

        recy.setLoadingMoreEnabled(true);
        recy.setPullRefreshEnabled(true);

        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getdata();
            }

            @Override
            public void onLoadMore() {
                getdata();
            }
        });

        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                page=1;
                keywords = search.getText().toString();
                getdata();
            }
        });

        getdata();

    }

    @Override
    protected void initData() {

    }
    private void getdata() {
        Map<String, String> map;
//1文章，2视频，3海报
            switch (type) {
                case "1":
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid", uid);
                    map.put("page", page + "");
                    if (keywords!=null&&!keywords.equals("")){
                        map.put("keyword", keywords);
                    }
                    pressenter.sendMessage(SearchActivity.this, Constant.article_list, map, MainArticleBean.class);
                    break;
                case "2":
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid", uid);
                    map.put("page", page + "");
                    if (keywords!=null&&!keywords.equals("")){
                        map.put("keyword", keywords);
                    }
                    pressenter.sendMessage(SearchActivity.this, Constant.videovideo_list, map, MainVideoBean.class);
                    break;
                case "3":
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid", uid);
                    map.put("page", page + "");
                    if (keywords!=null&&!keywords.equals("")){
                        map.put("keyword", keywords);
                    }
                    pressenter.sendMessage(SearchActivity.this, Constant.posterposter_list, map, MainPosterBean.class);
                    break;
                case "4":
                    map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid", uid);
                    map.put("page", page + "");
                    if (keywords!=null&&!keywords.equals("")){
                        map.put("keyword", keywords);
                    }
                    pressenter.sendMessage(SearchActivity.this, Constant.my_user_list, map, GetMyUserBean.class);
                    break;
                default:
            }

    }

    private String type="1";

    @OnClick({R.id.back, R.id.article, R.id.video, R.id.poster, R.id.user})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.article:
                type="1";
                page=1;
                posterText.setTextColor(Color.parseColor("#666666"));
                videoText.setTextColor(Color.parseColor("#666666"));
                userText.setTextColor(Color.parseColor("#666666"));
                userLine.setVisibility(View.INVISIBLE);
                posterLine.setVisibility(View.INVISIBLE);
                videoLine.setVisibility(View.INVISIBLE);
                articleText.setTextColor(getResources().getColor(R.color.main));
                articleLine.setVisibility(View.VISIBLE);
                getdata();
                break;
            case R.id.video:
                type="2";
                page=1;
                posterText.setTextColor(Color.parseColor("#666666"));
                articleText.setTextColor(Color.parseColor("#666666"));
                userText.setTextColor(Color.parseColor("#666666"));
                userLine.setVisibility(View.INVISIBLE);
                posterLine.setVisibility(View.INVISIBLE);
                articleLine.setVisibility(View.INVISIBLE);
                videoText.setTextColor(getResources().getColor(R.color.main));
                videoLine.setVisibility(View.VISIBLE);
                getdata();
                break;
            case R.id.poster:
                type="3";
                page=1;
                videoText.setTextColor(Color.parseColor("#666666"));
                articleText.setTextColor(Color.parseColor("#666666"));
                userText.setTextColor(Color.parseColor("#666666"));
                userLine.setVisibility(View.INVISIBLE);
                videoLine.setVisibility(View.INVISIBLE);
                articleLine.setVisibility(View.INVISIBLE);
                posterText.setTextColor(getResources().getColor(R.color.main));
                posterLine.setVisibility(View.VISIBLE);
                getdata();
                break;
            case R.id.user:
                type="4";
                page=1;
                videoText.setTextColor(Color.parseColor("#666666"));
                articleText.setTextColor(Color.parseColor("#666666"));
                posterText.setTextColor(Color.parseColor("#666666"));
                posterLine.setVisibility(View.INVISIBLE);
                videoLine.setVisibility(View.INVISIBLE);
                articleLine.setVisibility(View.INVISIBLE);
                userText.setTextColor(getResources().getColor(R.color.main));
                userLine.setVisibility(View.VISIBLE);
                getdata();
                break;
                default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof MainArticleBean) {
            final MainArticleBean mainArticleBean = (MainArticleBean) data;
            if (mainArticleBean.getCode() == 1) {
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    mainArticleBeanData = mainArticleBean.getData();

                    mainArticleAdapter = new MainArticleAdapter(SearchActivity.this);

                    mainArticleAdapter.setdeleteClick(new MainArticleAdapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            Intent intent = new Intent(SearchActivity.this, ArticleActivity.class);
                            intent.putExtra("article_id", mainArticleBean.getData().get(posttion).getId() + "");
                            intent.putExtra("coverImg",mainArticleBean.getData().get(posttion).getCoverImg()+"");
                            SearchActivity.this.startActivity(intent);

                        }
                    });

                    recy.setAdapter(mainArticleAdapter);
                }else {
                    mainArticleBeanData.addAll(mainArticleBean.getData());
                }
                mainArticleAdapter.setList(mainArticleBeanData);
                page++;
            } else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(SearchActivity.this, mainArticleBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
        } else if (data instanceof MainVideoBean) {
            final MainVideoBean mainVideoBean = (MainVideoBean) data;
            if (mainVideoBean.getCode() == 1) {
                if (page==1){
                    mainVideoBeanData = mainVideoBean.getData();
                    recy.setVisibility(View.VISIBLE);

                    mainVideoAdapter = new MainVideoAdapter(SearchActivity.this);
                    mainVideoAdapter.setdeleteClick(new MainVideoAdapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            Intent intent = new Intent(SearchActivity.this, VideoActivity.class);
                            intent.putExtra("video_id", mainVideoBean.getData().get(posttion).getId() + "");
                            intent.putExtra("coverImg",mainVideoBean.getData().get(posttion).getCoverImg()+"");
                            SearchActivity.this.startActivity(intent);
                        }
                    });
                    recy.setAdapter(mainVideoAdapter);
                }else {
                    mainVideoBeanData.addAll(mainVideoBean.getData());
                }
                mainVideoAdapter.setList(mainVideoBeanData);
                page++;

            } else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(SearchActivity.this, mainVideoBean.getMessage(), Toast.LENGTH_SHORT).show();
            }


        } else if (data instanceof MainPosterBean) {
            final MainPosterBean mainPosterBean = (MainPosterBean) data;
            if (mainPosterBean.getCode() == 1) {
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    mainPosterBeanData = mainPosterBean.getData();

                    mainPosterAdapter = new MainPosterAdapter(SearchActivity.this);

                    mainPosterAdapter.setdeleteClick(new MainPosterAdapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            Intent intent = new Intent(SearchActivity.this, PosterActivity.class);
                            intent.putExtra("poster_id", mainPosterBean.getData().get(posttion).getId() + "");
                            SearchActivity.this.startActivity(intent);
                        }
                    });

                    recy.setAdapter(mainPosterAdapter);
                }else {
                    mainPosterBeanData.addAll(mainPosterBean.getData());
                }
                mainPosterAdapter.setList(mainPosterBeanData);
                page++;

            } else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(SearchActivity.this, mainPosterBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }else if (data instanceof GetMyUserBean) {
            final GetMyUserBean getUserListBean = (GetMyUserBean) data;
            if (getUserListBean.getCode() == 1) {
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    userListBeanData = getUserListBean.getData();

                    userListAdapter = new UserListAdapter(SearchActivity.this);
                    userListAdapter.setdeleteClick(new UserListAdapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            b_position=posttion;
                            Map<String,String> map=new HashMap<>();
                            map.put("token",Constant.TOKEN);
                            map.put("type","3");
                            map.put("uid",userListBeanData.get(posttion).getUserId()+"");
                            map.put("id",uid);
                            pressenter.sendMessage(SearchActivity.this,Constant.add,map, Bean.class);
                        }
                    });
                    recy.setAdapter(userListAdapter);
                }else {
                    userListBeanData.addAll(getUserListBean.getData());
                }
                userListAdapter.setList(userListBeanData);
                page++;

            } else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(SearchActivity.this, getUserListBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if ((bean.getCode()==1)){
                userListBeanData.get(b_position).setIs_follow(1);
                userListAdapter.setList(userListBeanData);
            }


        }
        recy.refreshComplete();
        recy.loadMoreComplete();
    }

    private int b_position;

    @Override
    public void fail(String error) {

    }
}
